<?php
class Zendvn_Models_Notes extends Zend_Db_Table{
	protected $_name = 'notes';
	protected $_primary = 'id';
		
	public function listItem($arrParam = null, $options = null){										
		$db = $this->getAdapter();		
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];

		if($options['task'] == 'admin-list'){
			$group_id 	= $options['group_id'];
			$user_id	= $options['user_id'];
			$select = $db->select()
						 ->from($this->_name . ' AS n')
						 ->joinLeft('users AS u','u.id = n.created_by',array('u.user_name as user_name','u.group_id'));
						 
			if($group_id != 1){
				$select->where('n.created_by = ?',$user_id);
			}			 							
			if(!empty($ssFilter['col']) && !empty($ssFilter['order'])){
				$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);			
			}
			$result  = $db->fetchAll($select);
		}
		if($options['task'] == 'public'){
			$select = $db->select()
						 ->from($this->_name . ' AS u')
						 ->where('status = 1')
						 ->order('order ASC');
			$result  = $db->fetchRow($select);
		}
		return $result;
	
	}
	
	public function saveItem($arrParam = null, $options = null){
		$info = new Zendvn_System_Info();
		$memberInfo = $info->getMemberInfo('id');
		
		if($options['task'] == 'admin-add'){
			$row->created		= date("Y-m-d-G-i-s");
			$row->created_by 	= $memberInfo;
			$row =  $this->fetchNew();
		}
		
		if($options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];	
			$row->modified		= date("Y-m-d-G-i-s");
			$row->modified_by 	= $memberInfo;
			$row =  $this->fetchRow($where);
		}
		$row->name 		= $arrParam['name'];
		$row->content 	= $arrParam['content'];
		$row->selfoff 	= $arrParam['selfoff'];
		$row->order	 	= $arrParam['order'];
		$row->status 		= $arrParam['status'];
		$row->save();		
	}	
	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){
			$where = ' id = ' . $arrParam['id'];
			$this->delete($where);
		}		
		if($options['task'] == 'admin-multi-delete'){
			$cid = $arrParam['cid'];
			if(count($cid)>0){				
				$ids = implode(',',$cid);			
				$db = $this->getAdapter();
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
		}
	}

	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];		
		if(count($cid)>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}			
			$ids = implode(',',$cid);
			$data = array('status'=>$status);
			$where = 'id IN (' . $ids . ')';
			$this->update($data,$where);
		}
		if($arrParam['id']>0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}			
			$data = array('status'=>$status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data,$where);
		}		
	}
	public function sortItem($arrParam = null, $options = null){
		$items = $arrParam[$options['column']];
		if(count($items)>0){
			foreach ($items as $key => $val) {
				$where = 'id = ' . $key;				
				$data = array($options['column']=>$val);
				$this->update($data,$where);
			}
		}
	}
	public function getItem($arrParam = null, $options = null){		
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];
			$result = $this->fetchRow($where)->toArray();
		}
		return $result;
	}
	
}